using System;
using System.Collections.Generic;
using System.Windows;
using ApprovalTests;
using ApprovalTests.Reporters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SmallBasicFun.TicTacToeGame;

namespace SmallBasicFun.Tests.TicTacToe
{
	[TestClass]
	
	public class TicTacToeBoardTest
	{
		private Board GetBoard(int x0, int y0, int x1, int y1, int x2, int y2, string player)
		{
			var board = new Board();
			board.Click(player, x0, y0);
			board.Click(player, x1, y1);
			board.Click(player, x2, y2);
			return board;
		}
		[TestMethod]
		public void TestConvertXY()
		{
			Board b = new Board();
			Approvals.VerifyAll(GetAllXY(),p =>p+"=>"+ b.GetSquareByXY((int) p.X,(int) p.Y));
		}
		[TestMethod]
		public void TestConvertPoint()
		{
			Board b = new Board();
			var squares = new int[]{1,2,3,4,5,6,7,8,9};
			Approvals.VerifyAll(squares	,p =>p+"=>"+ b.GetXYBySquare(p));
		}

		public IEnumerable<Point> GetAllXY()
		{
			for (int y = 1; y <= 3; y++)
			{
				for (int x = 1; x <= 3; x++)
				{
					yield return new Point(x,y);
				}
			}
		}

		[TestMethod]
		public void TestCurrentPlayer()
		{
			var board = new Board();
			Assert.AreEqual("X", board.CurrentPlayer);
			board.Click("X", 1, 1);
			Assert.AreEqual("O", board.CurrentPlayer);
		}

		[TestMethod]
		public void TestOccupiedCell()
		{
			var board = new Board();


			board.Click("X", 0, 1);
			var points = new List<Point> {new Point(0, 1), new Point(0, 0), new Point(4, 4), new Point(-1, 0)};


			Approvals.VerifyAll(points, p => p + "=" + board.IsValid((int)p.X, (int)p.Y));
		}


		[TestMethod]
		public void TestTicTacToe()
		{
			var board = new Board();

			board.Click("X", 0, 1);
			Assert.IsFalse(board.IsGameWon("O"));
			board.Click("O", 1, 1);
			board.Click("X", 0, 2);
			board.Click("O", 0, 0);
			board.Click("X", 1, 2);
			board.Click("O", 2, 2);
			Assert.IsTrue(board.IsGameWon("O"));
			Approvals.Verify(board.ToString());
		}

		[TestMethod]
		public void TestVictoryConditions()
		{
			var boards = new List<Board>();

			// Rows
			boards.Add(GetBoard(0, 0, 1, 0, 2, 0, "O"));
			boards.Add(GetBoard(0, 1, 1, 1, 2, 1, "O"));
			boards.Add(GetBoard(0, 2, 1, 2, 2, 2, "O"));
			// Columns
			boards.Add(GetBoard(0, 0, 0, 1, 0, 2, "O"));
			boards.Add(GetBoard(1, 0, 1, 1, 1, 2, "O"));
			boards.Add(GetBoard(2, 0, 2, 1, 2, 2, "O"));
			// Diagonal
			boards.Add(GetBoard(0, 0, 1, 1, 2, 2, "O"));
			boards.Add(GetBoard(0, 2, 1, 1, 2, 0, "O"));

			boards.Add(GetBoard(0, 2, 1, 1, 2, 0, "Wi"));

			Approvals.VerifyAll(boards, b => b.ToString());
		}
	}
}